<template>
  <div class="mod-config">
    <el-form :inline="true" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input
          type="small"
          v-model="keyword"
          placeholder="请输入标题名"
          clearable
          @clear="getDataList()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <!--                <el-input type="small" v-model="keyword" placeholder="课程状态" clearable></el-input>-->
        <el-select
          v-model="value"
          placeholder="请选择课程状态"
          filterable
          clearable
          @clear="getDataList()"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input placeholder="请选择分类" :value="current.name" type="text" readonly>
          <template slot="prepend">
            <el-button class="el-icon-edit" @click="checkSort" type="text">选择分类</el-button>
          </template>
          <template slot="suffix">
            <el-button
              v-show="current.name"
              class="el-icon-delete-solid"
              @click="delectCate"
              type="text"
            >清空</el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">搜索</el-button>
      </el-form-item>
      <div>
        <el-form-item>
          <el-button type="primary" @click="addOrCurriculum()">课程新增</el-button>
        </el-form-item>
<!--        <el-form-item>-->
<!--          <el-button type="small" @click="sort">分类管理</el-button>-->
<!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-button type="small" @click="sort">分类管理</el-button>-->
        <!--        </el-form-item>-->
        <!--                <el-form-item>-->
        <!--                    <el-button v-if="isAuth('news:newsCategory:delete')" type="small" @click="deleteHandle()"-->
        <!--                               :disabled="dataListSelections.length <= 0">批量删除-->
        <!--                    </el-button>-->
        <!--                </el-form-item>-->
      </div>
    </el-form>
    <el-table
      :data="dataList"
      row-key="id"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column align="center" type="index" header-align="center" width="80px" label="序号"></el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="课程名称"></el-table-column>
      <el-table-column prop="cateName" label="课程分类" header-align="center" align="center"></el-table-column>
      <el-table-column prop="classStatus" label="课程状态" header-align="center" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            v-if="parseInt(scope.row.classStatus) === 0"
            @click="evaluateHandle(scope.row.id)"
          >发布</el-button>
          <el-tag v-if="scope.row.classStatus === 1">已发布</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrCurriculum(scope.row.id, scope.row.classStatus,1)">修改</el-button>
          <el-button
            v-if="scope.row.classStatus == 1"
            type="text"
            size="small"
            @click="adminiIstration(scope.row.id)"
          >管理</el-button>
          <el-button type="text" :loading="loading" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer title="重新发布" :visible.sync="drawer" :before-close="handleClose">
      <el-form>
        <el-form-item>
          <el-radio>发布上线</el-radio>
          <el-radio>课程下架</el-radio>
        </el-form-item>
      </el-form>
      <div>
        <el-button>取消</el-button>
        <el-button>确定</el-button>
      </div>
    </el-drawer>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 查看评价   -->
    <comment ref="comment" @refreshDataList="getDataList"></comment>
    <!--    <新增课程>-->
    <addCurriculum ref="addCurriculum" :getDataList="getDataList"></addCurriculum>
    <!--    课程管理-->
    <adminiIstration ref="adminiIstration" :getDataList="getDataList"></adminiIstration>

    <el-dialog
      title="分类管理"
      :visible.sync="dialogFormVisible"
      :show-close="false"
      append-to-body
      width="40%"
    >
      <learn-cate
        ref="learnCate"
        v-if="learnCate"
        type="试卷分类"
        :setCurrent="setCurrent"
        :current="current"
      />

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import learnCate from './learncate'
// import AddOrUpdate from './learn-vurriculum-add'
import Comment from './comment'
// import AddCurriculum from './../../../views/common/drawer'
import AddCurriculum from '@/views/common/drawer'
import AdminiIstration from './adminiIstration'

export default {
  data() {
    return {
      options: [
        {
          value: 0,
          label: '未发布'
        },
        {
          value: 1,
          label: '已发布'
        }
      ],
      selectOrGroup: false,
      fullscreenLoading: false,
      value: '',
      current: {},
      dialogFormVisible: false,
      learnCate: false,
      visible: false,
      drawer: false,
      keyword: '',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      loading: false,
      dataListLoading: false,
      dataListSelections: [],
      commentVisible: false,
      adddrawerVisible: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    Comment,
    AddCurriculum,
    learnCate,
    AdminiIstration
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 课程分类
    checkSort() {
      this.dialogFormVisible = true
      this.learnCate = true
      this.$nextTick(() => {
        this.$refs.learnCate.init()
      })
    },
    delectCate() {
      this.current = { name: '', id: '' }
      this.cateId = ''
      this.getDataList()
    },
    sort() {
      this.learnCate = true
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.learnCate.init()
      })
    },
    setCurrent(data) {
      if (data.id) this.current = data
      this.cateId = data.id
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    handleClick() {
      alert('是否取消发布')
    },
    // 获取数据列表
    getDataList(page) {
      if (page) this.pageIndex = page
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/train-admin/course/list'),
        method: 'post',
        data: {
          page: this.pageIndex,
          limit: this.pageSize,
          title: this.keyword,
          cateId: this.cateId,
          classStatus: this.value
        }
      }).then(res => {
        if (res && res.code === 200) {
          this.dataList = res.data.list
          this.totalPage = res.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    addOrCurriculum(id, classStatus, addoredit) {
      this.$nextTick(() => {
        this.$refs.addCurriculum.init(id, classStatus)
      })
    },
    // 管理
    adminiIstration(id) {
      this.$nextTick(() => {
        this.$refs.adminiIstration.init(id)
      })
    },
    commentHandle(id) {
      this.commentVisible = true
      this.$nextTick(() => {
        this.$refs.comment.init(id)
      })
    },
    evaluateHandle(id) {
      this.$alert('课程发布后将无法对信息进行修改，是否继续发布？', '提示', {
        confirmButtonText: '发布',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/train-admin/course/updatecoursestatus'),
            method: 'post',
            data: {
              id: id,
              classStatus: 1
            }
          }).then(data => {
            if (data && data.code === 200) {
              this.$notify.success({
                title: '提示',
                message: '操作成功'
              })
              this.getDataList()
            } else {
              this.$notify.warning({
                title: '提示',
                message: data.msg
              })
            }
          })
        })
        .catch(_ => {
          this.$notify.info({
            title: '提示',
            message: '已取消发布'
          })
        })
    },
    // 删除
    deleteHandle(id) {
      // var ids = id ? [id] : this.dataListSelections.map(item => {
      //   return item.id
      // })
      // this.fullscreenLoading = true
      this.$confirm(`确定要进行删除操作吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/train-admin/course/delete'),
          method: 'post',
          data: { id: id }
        }).then(res => {
          if (res && res.code === 200) {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success'
            })
            this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex --
            this.getDataList()
          } else {
            this.$notify.error({
              title: '提示',
              message: res.msg
            })
          }
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
